package com.scalar.forecast.projectSetup.actionCommands;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.scalar.forecast.projectSetup.forms.ProjectSetupForm;
import com.scalar.forecast.projectSetup.dao.ProjectSetupDAO;
import com.scalar.forecast.projectSetup.dao.UserDefinedFieldsDAO;
import com.scalar.forecast.projectSetup.ProjectSetup;
import com.scalar.forecast.commons.SessionKeys;
import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.forecastEntry.dao.ForecastEntrySearchDAO;
import com.scalar.forecast.salesPersonSetup.dao.SalesPersonSetupDAO;
import com.scalar.chiptrack.sales.dao.CustomerRelationshipDAO;

import java.util.ArrayList;
import java.util.HashMap;


/**
 * Processes the request to display the project setup details for modification based on projecId value passed
 *
 * Created by Veeru. 
 * Date: Feb 9, 2005
 * Time: 7:02:02 PM 
 */

public class ProjectSetupModifyCommand extends ActionCommand{

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) {

        ActionForward actionForward = mapping.findForward( "projectSetup" );

        ProjectSetupForm projectSetupForm = ( ProjectSetupForm )form;
        ProjectSetup projectSetup = projectSetupForm.getProjectSetup();
        int projectId = 0;
        HashMap errors = new HashMap ();
        HashMap info = new HashMap ();
        HttpSession session = request.getSession();
        request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
        request.setAttribute( SessionKeys.INFO_MESSAGES, info );
        if ( request.getParameter("projectID") != null ) {
            projectId = Integer.parseInt( request.getParameter("projectID").trim() );
        }
        else if ( projectSetup.getProjectID() > 0 )
        {
            projectId = projectSetup.getProjectID();
            info.put( "The part numbers are replaced for this project successfully", null );
        }
        if ( projectId > 0 ) {
            try {
                projectSetup.setProjectID( projectId );
                request.setAttribute( "customers", ForecastEntrySearchDAO.getAllCustomers( null ) );
                ProjectSetupDAO.loadProjectSetupDetails( null, projectSetup );
                request.setAttribute( "odmsList", CustomerRelationshipDAO.getOdmsForChangeSalesOrder( projectSetup.getCustomerID()+"", projectSetup.getOdmID()+"" ) );
                request.setAttribute( "endUserList", CustomerRelationshipDAO.getEndCustomersForChangeSalesOrder( projectSetup.getCustomerID()+"", projectSetup.getEndUserId()+"" ) );
                request.setAttribute( "salesPersons", SalesPersonSetupDAO.getSalesPersonList() );

                request.setAttribute( "availableChipsList" , ProjectSetupDAO.loadAvailableProducts( null, projectId, "CHIP") );
                request.setAttribute( "availableBoardsList" , ProjectSetupDAO.loadAvailableProducts( null, projectId, "BOARD") );
                request.setAttribute( "availableKitsList" , ProjectSetupDAO.loadAvailableProducts( null, projectId, "KIT") );

                request.setAttribute( "selectedChipsList" , ProjectSetupDAO.loadSelectedProducts( null, projectId, "CHIP" ) );
                request.setAttribute( "selectedBoardsList" , ProjectSetupDAO.loadSelectedProducts( null, projectId, "BOARD" ) );
                request.setAttribute( "selectedKitsList" ,  ProjectSetupDAO.loadSelectedProducts( null, projectId, "KIT" ) );

                session.setAttribute( "customerCompulsory", ProjectSetupDAO.isCustomerSelected( null )+"" );

                ArrayList userDefinedfields = UserDefinedFieldsDAO.loadUserDefinedFieldValues( null, projectId, "PROJSETUP" );
                projectSetup.setUserDefinedFeilds( userDefinedfields );

            } catch ( Exception e ) {
                e.printStackTrace();
                errors.put( "Error while loading the project setup details", null );
                errors.put( ERROR_INTERNAL_SERVER_ERROR, null);
                actionForward = mapping.findForward( "projectSearch" );
            }
        }
        return actionForward;
    }
}
